﻿using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;

namespace JQMergeFramework
{
    public class CityPool : MonoBehaviour
    {
        public static CityPool I;
        
        public GameObject cloneObject;
        public Stack<CityUnit> stack = new Stack<CityUnit>();

        public int cacheCount;

        //初始缓存数量, 防止瞬间大量调用的问题
        public int initCacheSize = 10;

        private void Awake()
        {
            I = this;
            if (cloneObject == null)
            {
                cloneObject = transform.GetChild(0).gameObject;
                cloneObject.SetActive(false);
            }

            if (transform.childCount <= initCacheSize)
            {
                for (int i = 0; i < initCacheSize; i++)
                {
                    CityUnit unit = Instantiate(cloneObject).GetComponent<CityUnit>();
                    unit.transform.SetParent(transform);
                    Put(unit);
                }
            }
        }
        
        public void PutAll()
        {
            for (int i = 0; i < transform.childCount; i++)
            {
                Transform obj = transform.GetChild(i);
                if (obj.gameObject.activeSelf) Put(obj.GetComponent<CityUnit>());
            }
        }

        //放入对象池中的对象
        public void Put(CityUnit unit)
        {
            if (unit == null)
            {
                return;
            }

     
            unit.GetComponent<BhvEasyPool.IPoolRecycle>()?.OnRecyclePut();
            unit.gameObject.SetActive(false);

            if (unit.transform != transform)
            {
                unit.transform.SetParent(transform); //回收设置为此对象内部
            }

            if (stack.Contains(unit) == false)
            {
                stack.Push(unit);
            }

            cacheCount = transform.childCount;
        }

        //延迟执行put
        public void Put(CityUnit obj, float time)
        {
            StartCoroutine(PutDelay(obj, time));
        }

        private IEnumerator PutDelay(CityUnit obj, float time)
        {
            yield return new WaitForSeconds(time);
            Put(obj);
        }


        //获得对象池中的对象
        public CityUnit Get()
        {
            CityUnit obj;
            if (stack.Count <= 0)
            {
                obj = Instantiate(cloneObject).GetComponent<CityUnit>();
            }
            else
            {
                obj = stack.Pop();
            }

            obj.gameObject.SetActive(true);
            obj.transform.SetParent(transform);
            //重设默认值
            obj.transform.localScale = cloneObject.transform.localScale;
            obj.transform.rotation = cloneObject.transform.rotation;
            obj.transform.position = cloneObject.transform.position;

            obj.GetComponent<BhvEasyPool.IPoolRecycle>()?.OnRecycleGet();

            return obj;
        }

        public void ClearAll()
        {
            stack.Clear();
            int childCount = transform.childCount;
            for (int i = 0; i < childCount; i++)
            {
                GameObject obj = transform.GetChild(i).gameObject;
                if (obj != this.cloneObject)
                {
                    DOTween.Kill(obj.GetInstanceID());
                    GameObject.Destroy(obj);
                }
            }
        }

     

        private void OnDestroy()
        {
            foreach (CityUnit go in stack)
            {
                DOTween.Kill(go.gameObject.GetInstanceID());
            }

            stack.Clear();
        }
    }
}